// DepartmentRepository.java
package com.hibernate.hrm.dao;

import com.hibernate.hrm.entity.Department;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface DepartmentRepository extends JpaRepository<Department, Integer> {
    // 查找所有顶级部门（没有父部门的部门）
    List<Department> findByDepartmentParentIDIsNull();

    // 根据父部门查找子部门
    List<Department> findByDepartmentParentID(Department parentDepartment);

    @Query("SELECT COUNT(u) FROM User u WHERE u.departmentID = :department AND u.status = true")
    long countActiveEmployeesByDepartment(@Param("department") Department department);

    @Query("SELECT d FROM Department d WHERE d.departmentID <> :currentId AND d.status = true")
    List<Department> findAvailableParentDepartments(@Param("currentId") Integer currentId);

    boolean existsByDepartmentCode(String departmentCode);

    List<Department> findByStatusTrue();
}